const zc = $(".zc");
const xgbtn = document.getElementById("xg");
const uitemh=document.getElementById("u-item-h");
xgbtn.onclick = function () {
    if (xgbtn.value === "修改信息") {
        uitemh.style.display="";
        for (let i = 0; i < zc.length; i++) {
            zc.eq(i).removeAttr('readonly');
            zc.eq(i).css('background-color',"#eee");
        }
        xgbtn.value = "取消"
        return;
    }
    if (xgbtn.value === "取消") {
        uitemh.style.display="none";
        for (let i = 0; i < zc.length; i++) {
            zc.eq(i).attr('readonly','readonly');
            zc.eq(i).css('background-color',"")
        }
        xgbtn.value = "修改信息"
    }
    if ( xgbtn.value === "保存"){
        for (let j = 0; j < zc.length; j++) {
            if ( zc.eq(j).val()===""){
                zc.eq(j).addClass("input change");
                zc.eq(j).attr("placeholder","不能为空!");
                return;
            }
        }

        if($("#newpwd").val()!=="") {
            if($("#rnewpwd").val()===""){
                $("#rnewpwd").addClass("input change");
                $("#rnewpwd").attr("placeholder","重复密码不能为空!");
                return;
            }
        }

        if($("#rnewpwd").val()!==""&&$("#newpwd").val()!==""){
            if($("#rnewpwd").val()!==$("#newpwd").val()){
                $("#rnewpwd").val(null)
                $("#rnewpwd").addClass("input change");
                $("#rnewpwd").attr("placeholder","两次密码不一致!");
                return;
            }
            const reg=/^(?=.*?[a-zA-Z])(?=.*?\d)[a-zA-Z\d]{7,15}$/
            if (!reg.test($("#newpwd").val())){
                $("#newpwd").val(null);
                $("#rnewpwd").val(null);
                $("#newpwd").addClass("input change");
                $("#newpwd").attr("placeholder","格式错误(7-15位字母数字组合)");
                return;
            }
        }
        // 发送Ajax请求
        $.ajax({
            url: "/admins/UpAdminInfo",
            type: "post",
            data: {
                pwd:  zc.eq(3).val(),
                Npwd: $("#newpwd").val(),
                name:  zc.eq(0).val(),
                age: zc.eq(1).val(),
                gender: zc.eq(2).val(),
                tel:$("#tel").val(),
                verifyCode: $("#verifyCode").val()
            },
            success: function (Result) {
                zc.eq(3).val(null);
                $("#newpwd").val(null);
                $("#rnewpwd").val(null);
                $("#verifyCode").val(null);
                if(Result.code===40021){
                    xgbtn.value = "修改信息";
                    uitemh.style.display="none";
                }
                alert(Result.msg);
                const time = new Date().getTime(); // 获取当前时间戳
                const verifyCodeImg = document.querySelector('#verifyCodeImg');
                verifyCodeImg.src = '/verifyCode?t=' + time; // 设置验证码图片地址
            },
            error: function () {
                alert("请求失败");
            }
        });
    }
}

for (let j = 0; j < zc.length; j++) {
    zc[j].addEventListener("change", function () {
        xgbtn.value = "保存";
        const reg = /^[^\u4e00-\u9fa5 ]{7,16}$/;
        if (zc.eq(j).val()===""){
            zc.eq(j).addClass("input change");
            zc.eq(j).attr("placeholder","不能为空!");
        }
    });
}

function refreshVerifyCode() {
    const time = new Date().getTime(); //获取当前时间戳
    const verifyCodeImg = document.querySelector('#verifyCodeImg');
    verifyCodeImg.src = '/verifyCode?t=' + time; //设置验证码图片地址
}
window.onload = function () {
    const time = new Date().getTime(); // 获取当前时间戳
    const verifyCodeImg = document.querySelector('#verifyCodeImg');
    verifyCodeImg.src = '/verifyCode?t=' + time; // 设置验证码图片地址
}